package cn.finder.pr;


import java.io.IOException;

import org.apache.log4j.Logger;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.FalsifyingWebConnection;

/***
 * 页面静态化
 * @author finder
 * example:http://htmlunit.sourceforge.net/gettingStarted.html
 */
public class FinderHtmlUnit {
	
	private static Logger logger=Logger.getLogger(FinderHtmlUnit.class);
	
	private WebClient webClient;  
  
    private int timeout; //默认最长 20秒
    
    private int retryCollect=0;//默认为0 不进行重新收集
    
    private WaitLoadCompleteHandler waitLoadCompleteHandler;
    
    public FinderHtmlUnit(){
    	this(20);
    }
    public FinderHtmlUnit(int timeout){
    	 this.timeout=timeout;
    	 webClient = new WebClient(BrowserVersion.getDefault());  
    	 webClient.setAjaxController(new NicelyResynchronizingAjaxController());// 设置ajax代理 
    	 webClient.setJavaScriptTimeout(timeout / 5 * 1000);  
         webClient.waitForBackgroundJavaScript(timeout * 1000);
         //webClient.waitForBackgroundJavaScriptStartingBefore(timeout * 1000);
         //waitLoadCompleteHandler=new DefaultWaitLoadCompleteHandler();
         new SimpleConectionListener(webClient);
         
         init();//初始化配置
    }
    
    public void setWaitLoadCompleteHandler(WaitLoadCompleteHandler waitLoadCompleteHandler){
    	this.waitLoadCompleteHandler=waitLoadCompleteHandler;
    }
    
    /***
     * 设置 发送异常后，重新收集的次数
     * @param retryCollect
     */
    public void setRetryCollect(int retryCollect){
    	this.retryCollect=retryCollect;
    }
    /***
     *  单位为秒
     * @param timeout
     */
    public void init() {  
    	
    	initOptions();
    }  
    
    private void initOptions(){
    	 webClient.getOptions().setJavaScriptEnabled(true);  
         webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);  
         webClient.getOptions().setThrowExceptionOnScriptError(false);  
         webClient.getOptions().setCssEnabled(false);
         webClient.getOptions().setUseInsecureSSL(true); 
       //  webClient.getOptions().setDoNotTrackEnabled(false);
         
         webClient.getOptions().setTimeout(timeout * 1000);
        
    }
  
  
    public HtmlPage collect(String url) {  
    	HtmlPage page=null;
    	try {
            page = (HtmlPage) webClient.getPage(url);
            
           // Thread.sleep(timeout/2*1000);//主要是这个线程的等待 因为js加载也是需要时间的
            //这里可以判断条件
            for (int i = 0; i < timeout*2; i++) {
            	if(waitLoadCompleteHandler==null){
            		waitLoadCompleteHandler=new DefaultWaitLoadCompleteHandler();
            	}
                if (waitLoadCompleteHandler.loadCompletedHandler(page, i, timeout)) {
                    break;
                }
                synchronized (page) {
                    page.wait(500);
                }
            }
            
            page.wait(5000);//防止 还有未加载的数据
        } catch (Exception e) {
        	if(retryCollect>0){
        		retryCollect-=1;
        		logger.info("====retry:"+retryCollect);
        		page=collect(url);
        	}
        	
        }  
        return page;
    }  
    
    public WebClient getWebClient(){
    	return this.webClient;
    }
    
    
    /** 
     * @author 很多时候我们想看看HtmlUnit到底去拿了什么东西，或者说我想对它拿的东西过滤一下，再或者我想把它拿到的某些东西存起来，那这个时候响应监听就很必要了。比如说一个最简单的响应监听。
     * @date 2014年9月15日 上午10:59:30 
     * @version $Id$ 
     * 
     */  
    public static class SimpleConectionListener extends FalsifyingWebConnection {  
      
      
        public SimpleConectionListener(WebClient webClient) throws IllegalArgumentException {  
            super(webClient);  
        }  
      
        @Override  
        public WebResponse getResponse(WebRequest request) throws IOException {  
            // 得到了这个响应，你想怎么处理就怎么处理了，不多写了  
      
            WebResponse response = super.getResponse(request);  
            String url = response.getWebRequest().getUrl().toString();  
      
            if (logger.isDebugEnabled()) {  
            	logger.debug("下载文件链接：" + url);  
            }  
            if (check(url)) {  
                return createWebResponse(response.getWebRequest(), "", "application/javascript", 200, "Ok");  
            }  
            return response;  
        }  
      
        private boolean check(String url) {  
            // TODO 加入你自己的判断什么的  
            return false;  
        }  
      
    } 
    
    
    
    
    
}
